package com.cskaoyan.gateway.controller.pay;


import com.cskaoyan.gateway.form.pay.PayForm;
import com.mall.commons.result.ResponseData;
import com.mall.commons.result.ResponseUtil;
import com.mall.pay.PayCoreService;
import com.mall.pay.constants.PayReturnCodeEnum;
import com.mall.pay.dto.PaymentRequest;
import com.mall.pay.dto.alipay.AlipayQueryRetResponse;
import com.mall.pay.dto.alipay.AlipaymentResponse;
import com.mall.user.dto.UsernameIdToken;
import com.mall.user.intercepter.TokenIntercepter;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @author: lsy
 * @class: PayController
 */
@RestController
public class PayController {

    @Reference(timeout = 30000, check = false, retries = 0)
    PayCoreService payCoreService;

    @PostMapping("/cashier/pay")
    public ResponseData pay(@RequestBody PayForm payForm, HttpServletRequest request) {
        UsernameIdToken usernameIdToken = (UsernameIdToken) request.getAttribute(TokenIntercepter.USER_INFO_KEY);
        long userId = usernameIdToken.getUserId();

        PaymentRequest paymentRequest = new PaymentRequest();
        // 地址id
//        paymentRequest.setAddressId();
        // 订单总金额
        paymentRequest.setOrderFee(payForm.getMoney());
        // 支付方式
        paymentRequest.setPayChannel(payForm.getPayType());
        // 终端ip 非必填
//        paymentRequest.setSpbillCreateIp();
        // 商品描述
        paymentRequest.setSubject(payForm.getInfo());
        // 实际支付金额(单位为分)
        paymentRequest.setTotalFee(payForm.getMoney());
        // 订单号
        paymentRequest.setTradeNo(payForm.getOrderId());
        paymentRequest.setUserId(userId);

        AlipaymentResponse alipaymentResponse = payCoreService.aliPay(paymentRequest);
        if (PayReturnCodeEnum.SUCCESS.getCode().equals(alipaymentResponse.getCode())) {
            return new ResponseUtil<>().setData("http://localhost:8080/image/" + alipaymentResponse.getQrCode());
        }
        return new ResponseUtil<>().setErrorMsg(alipaymentResponse.getMsg());
    }

    @GetMapping("/cashier/queryStatus")
    public ResponseData queryStatus(String orderId, HttpServletRequest request) {
        UsernameIdToken usernameIdToken = (UsernameIdToken) request.getAttribute(TokenIntercepter.USER_INFO_KEY);
        long userId = usernameIdToken.getUserId();

        PaymentRequest paymentRequest = new PaymentRequest();
        paymentRequest.setUserId(userId);
//        paymentRequest.set
        paymentRequest.setTradeNo(orderId);
        AlipayQueryRetResponse alipayQueryRetResponse = payCoreService.queryAlipayRet(paymentRequest);
        if (PayReturnCodeEnum.SUCCESS.getCode().equals(alipayQueryRetResponse.getCode())) {
            return new ResponseUtil<>().setData(null);
        }
        return new ResponseUtil<>().setErrorMsg(alipayQueryRetResponse.getMsg());

    }


}
